package com.hubt.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hubt.common.PageBean;
import com.hubt.mapper.PtOrganMapper;
import com.hubt.mapper.PtRUserDutyOrgMapper;
import com.hubt.mapper.PtUserDao;
import com.hubt.mapper.PtUserMapper;
import com.hubt.model.PtOrgan;
import com.hubt.model.PtRUserDutyOrg;
import com.hubt.model.PtUser;
import com.hubt.service.PtUserService;
import com.hubt.vo.Dutyvo;
import com.hubt.vo.UserDuty;
import com.hubt.vo.Uservo;

@Service("ptUserService")
public class PtUserServiceImpl implements PtUserService {

	@Autowired
	private PtUserMapper ptuserMapper;
	
	@Autowired
	PtUserDao ptUserDao;
	
	@Autowired PtOrganMapper ptorganMapper;
	
	@Autowired PtRUserDutyOrgMapper prtdmapper;
	
	@Override
	public PageBean<Uservo> findByPage(int pageNumber, int pageSize) {
		// TODO Auto-generated method stub
		
		List<PtUser> list=ptuserMapper.findAllByPage((pageNumber-1)*pageSize,pageSize);
		List<Uservo> uservos=new ArrayList<Uservo>();
		
		for(PtUser ptUser:list) {
			Uservo uservo=new Uservo();
			uservo.setUser_uuid(ptUser.getUserUuid());
			uservo.setUsername(ptUser.getUsername());
			uservo.setEmail(ptUser.getEmail());
			uservo.setIs_adminShort(ptUser.getIsAdmin());
			uservo.setModtime(ptUser.getModtime());
			uservo.setNice_name(ptUser.getNiceName());
			uservo.setPassword(ptUser.getPassword());
			uservo.setPwdans(ptUser.getPwdans());
			uservo.setPwdque(ptUser.getPwdque());
			uservo.setMobile(ptUser.getMobile());
			uservo.setOrgan_uuid(ptUser.getOrganUuid());
			uservo.setRemark(ptUser.getRemark());
			if(ptorganMapper.selectByPrimaryKey(ptUser.getOrganUuid())==null) {
				uservo.setOrgan_name(null);
			}else {
				uservo.setOrgan_name(ptorganMapper.selectByPrimaryKey(ptUser.getOrganUuid()).getOrganName());
			}
			//uservo.setOrgan_name(ptorganMapper.selectByPrimaryKey(ptUser.getOrganUuid()).getOrganName());
			uservos.add(uservo);
		}
		PageBean<Uservo> pageBean=new PageBean<Uservo>();
		pageBean.setRows(uservos);
		int total=ptuserMapper.countSize();
		pageBean.setTotal(total);
		pageBean.setPageNumber(pageNumber);
		pageBean.setPageSize(pageSize);
		pageBean.setTotalPage((total%pageNumber==0)?(total/pageNumber):(total/pageSize+1));
		return pageBean;
		
	}
    
	public void updateUser(UserDuty uservo) {
		PtUser ptUser = new PtUser();
		ptUser.setUserUuid(uservo.getUser_uuid());
		ptUser.setUsername(uservo.getUsername());
		ptUser.setPassword(uservo.getPassword());
		ptUser.setOrganUuid(uservo.getOrgan_uuid());
		ptUser.setEmail(uservo.getEmail());
		ptUser.setMobile(uservo.getMobile());
		ptUser.setNiceName(uservo.getNice_name());
		ptUser.setIsAdmin(uservo.getIs_adminShort());
		ptUser.setPwdque(uservo.getPwdque());
		ptUser.setPwdans(uservo.getPwdans());
		ptUser.setRemark(uservo.getRemark());
		if(!(prtdmapper.findByuser(uservo.getUser_uuid())==null)) {
			prtdmapper.deleteUser(uservo.getUser_uuid());
		}
		if(!(uservo.getDuties()==null)) {
		for(Dutyvo id:uservo.getDuties()) {
			PtRUserDutyOrg dutyOrg=new PtRUserDutyOrg();
			dutyOrg.setUserUuid(uservo.getUser_uuid());
			dutyOrg.setUserDutyid("ud"+(int)(1+Math.random()*100));
			if(id.getDutyId()==null) {
				
			}else {
				dutyOrg.setDutyid(id.getDutyId());
				prtdmapper.insert(dutyOrg);
			}
		}
		}
		ptuserMapper.updateByPrimaryKeySelective(ptUser);
	}

	
	public void addUser(UserDuty uservo) {
		PtUser ptuser = new PtUser();
		uservo.setStatus("N");
		ptuser.setUserUuid(uservo.getUser_uuid());
		ptuser.setStatus(uservo.getStatus());
		java.util.Date a = new java.util.Date();
		uservo.setModtime(a);
		ptuser.setModtime(uservo.getModtime());
		ptuser.setOrganUuid(uservo.getOrgan_uuid());
		ptuser.setNiceName(uservo.getNice_name());
		ptuser.setUsername(uservo.getUsername());
		ptuser.setPassword(uservo.getPassword());
		ptuser.setEmail(uservo.getEmail());
		ptuser.setMobile(uservo.getMobile());
		ptuser.setIsAdmin(uservo.getIs_adminShort());
		ptuser.setPwdque(uservo.getPwdque());
		ptuser.setPwdans(uservo.getPwdans());
		ptuser.setRemark(uservo.getRemark());
		System.out.println(uservo.getDuties());
		if(!(uservo.getDuties()==null)) {
		for(Dutyvo id:uservo.getDuties()) {
			PtRUserDutyOrg dutyOrg=new PtRUserDutyOrg();
			dutyOrg.setUserUuid(uservo.getUser_uuid());
			dutyOrg.setUserDutyid("ud"+(int)(1+Math.random()*100));
			if(id.getDutyId()==null) {
				
			}else {
				dutyOrg.setDutyid(id.getDutyId());
				prtdmapper.insert(dutyOrg);
			}
		}
		}
		ptuserMapper.insert(ptuser);
	}

	
	public void deleteUser(String user_uuid) {
		
		if(prtdmapper.findByuser(user_uuid)==null) {
			ptUserDao.deleteOne(user_uuid);
		}else {
			prtdmapper.deleteUser(user_uuid);
			ptUserDao.deleteOne(user_uuid);
		}
		
	}


	
	public PtUser getUserById(String user_uuid) {
	
		return ptUserDao.findOne(user_uuid);
	}
}
	
